package com.mialab.servlet_demo;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletConfigDemoServlet extends HttpServlet { 
	private ServletConfig servletConfig;
	
	@Override
    public void init(ServletConfig config) throws ServletException {
    	this.servletConfig = config;
    	System.out.println("-----------" + servletConfig + "-----------");
	}
    
    @Override
    public ServletConfig getServletConfig() {
        return servletConfig;
    }
    
    @Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 使用ServletConfig对象获取初始化参数
		ServletConfig servletConfig = getServletConfig();
		System.out.println("-----------" + servletConfig + "-----------");
		String poet = servletConfig.getInitParameter("poet");
		String poem = servletConfig.getInitParameter("poem");
		// 设置响应到客户端的文本类型为HTML
		response.setContentType("text/html;charset=UTF-8");
		// 获取输出流
		PrintWriter out = response.getWriter();
		out.print("<p>获取ServletConfigDenoServlet的初始化参数：");
		out.println("</p><p>poet参数的值：" + poet);
		out.println("</p><p>poem参数的值：" + poem + "</p>");		
		out.append("Served at：").append(request.getContextPath());
	}

}
